﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;

namespace Catsland.Core
{
	public class MaterialList : UniqueList<Material>
	{
		public void AddMaterial(Material material)
		{
			base.AddItem(material._name, material);

			if (Mgr<GameEngine>.Singleton._gameEngineMode
				== GameEngine.GameEngineMode.MapEditor)
			{
				Mgr<GameEngine>.Singleton.Editor.UpdateMaterialList(m_list);
			}
		}

		public Material GetMaterial(String name)
		{
			return base.GetItem(name);
		}

		public static MaterialList LoadFromNode(XmlNode node, Scene scene)
		{
			MaterialList newMaterialList = new MaterialList();

			foreach (XmlNode material in node.ChildNodes)
			{
				Material newMaterial = Material.LoadFromNode(material, scene);
				newMaterialList.AddMaterial(newMaterial);
			}

			return newMaterialList;
		}

		public bool SaveToNode(XmlNode node, XmlDocument doc)
		{
			
			XmlElement materials = doc.CreateElement("Materials");
			node.AppendChild(materials);

			foreach (KeyValuePair<String,Material> pair in m_list)
			{
				pair.Value.SaveToNode(materials, doc);
			}

			return true;
		}
	}
}
